<?php

/**
 * @package Nina
 * @author John Main <john@thedaddy.org>
 */
class Nina_Orm_Dummy extends Nina_Model
{
	/*! Table name (required) */
	const TABLE_NAME = "";
	
	/*! PK field (required) */
	const ID = "";

	/**
	 * The mimic model
	 * @var Nina_Model
	 */
	protected $mimic;

	/**
	 * Constructor
	 * @param Nina_Model $mimic_class The model class we will create a dummy of
	 */
	public function __construct($mimic_class) {
		$this->mimic = new $mimic_class();
	}

	/**
	 * Respond to the same calls the mimic class would but with dummy elements
	 * @param string $method Method name
	 * @param array $args The arguments
	 * @return mixed
	 */
	public function __call($method, $args) {
		if(substr($method, 0, 3) == "get") {
			$field = substr($method, 3);

			foreach ($this->mimic->getForeignKeys() as $actual_field => $orm_class) {
				if($field == $orm_class) return new Nina_Orm_Dummy($orm_class);
			}

			foreach ($this->mimic->getHasManyThrough() as $alias => $details) {
				if($field == $alias) return array(new Nina_Orm_Dummy($details[0]));
			}
			
			return $this->callHelper($method, $args);
		}

		return $this->callHelper($method, $args);
	}
	
	/**
	 * Produce suitable responses to any calls that might arrive
	 * @param string $method Method name
	 * @param array $args The arguments
	 * @return mixed
	 */
	protected function callHelper($method, $args) {
		
		if(in_array($method, get_class_methods(get_class($this->mimic)))) {
			return call_user_func_array(array($this->mimic, $method), $args);
		}
		
		if(substr($method, 0, 3) == "get")
			return '&lt;&lt;Record Not Found&gt;&gt;';
		else
			return true;
	}

	/**
	 * Declare this to be a dummy model
	 * @return true
	 */
	public function isDummy() {
		return true;
	}
}
